M2.959 · Visualització de dades · PRA 2
2022-1 · Màster universitari en Ciència de dades (Data science)
Estudis de Informàtica, Multimèdia i Telecomunicació
</u>
import folium
import pandas as pd
import geopandas as gpd
from folium.features import GeoJsonTooltip
import plotly.express as px
import statsmodels
import branca.colormap as cm
En aquesta segona part de la pràctica analitzarem a través de diverses de visualitzacions la relació entre determinats aspectes morfològics del teixit urbà de la ciutat de Barcelona amb indicadors socioeconòmics com la renda mitja neta per habitant, el preu del lloguer d’habitatge o el preu de venda de l’habitatge.
El punt de partida és un conjunt de dades de la pàgina Open Data de l’Ajuntament de Barcelona, format per 5298 observacions que es corresponen amb totes les illes que conformen el teixit urbà de la ciutat. Cada observació té 20 camps, entre els quals tenim el nom del barri i del districte, el nombre d’habitants, el nombre de parcel·les, i el nombre d’habitatges, de locals comercials o de pàrkings, entre d’altres.
Creuarem aquestes dades amb altres datasets per oferir una visió més amplia sobre com estan relacionades les variables urbanístiques amb el nivell de renda o el preu de l’habitatge. Per analitzar la ciutat des de diferents punts de vista, hem dividit la pràctica en 5 grans blocs: densitat, distància, vegetació, parcel·lari i usos.
Per poder combinar les dades urbanístiques, que estan mesurades per illes, amb la resta de dades, que estan organitzades per barris, farem una agregació de les dades de les illes agrupant-les per barris.
# Carreguem el dataset original.
illes = pd.read_csv("datasets\TAULA_MAP_ILLA.csv")
illes.head()
| DISTRICTE | CODI_ILLA | SECC_CENS | BARRI | NOM_DISTRICTE | NOM_BARRI | AEB | HABITANTS | NUM_PARC | NUM_LOCALS | NUM_VIVENDES | NUM_PARKINGS | NUM_ALTRES | SUP_CONS | SUP_SOBRE_RASANT | SUP_SOTA_RASANT | SUP_LOCALS | SUP_SERVEIS | SUP_SOL | DATA_DADES | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 5071 | 001 | 01 | Ciutat Vella | el Raval | 001 | 0 | 4 | 96 | 1 | 3 | 89 | 202635 | 94173 | 108462 | NaN | NaN | 114001 | 02/12/2022 00:02:16 |
| 1 | 1 | 6041 | 036 | 03 | Ciutat Vella | la Barceloneta | 014 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | NaN | NaN | 8360 | 02/12/2022 00:02:16 |
| 2 | 1 | 6044 | 036 | 03 | Ciutat Vella | la Barceloneta | 014 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | NaN | NaN | 4843 | 02/12/2022 00:02:16 |
| 3 | 1 | 6054 | 036 | 03 | Ciutat Vella | la Barceloneta | 014 | 0 | 3 | 3 | 0 | 0 | 3 | 3711 | 0 | 3711 | NaN | NaN | 62393 | 02/12/2022 00:02:16 |
| 4 | 1 | 6055 | 036 | 03 | Ciutat Vella | la Barceloneta | 014 | 0 | 4 | 697 | 60 | 563 | 74 | 94256 | 55392 | 38864 | NaN | NaN | 13401 | 02/12/2022 00:02:16 |
illes.dtypes
DISTRICTE int64 CODI_ILLA int64 SECC_CENS object BARRI object NOM_DISTRICTE object NOM_BARRI object AEB object HABITANTS int64 NUM_PARC int64 NUM_LOCALS int64 NUM_VIVENDES int64 NUM_PARKINGS int64 NUM_ALTRES int64 SUP_CONS int64 SUP_SOBRE_RASANT int64 SUP_SOTA_RASANT int64 SUP_LOCALS float64 SUP_SERVEIS float64 SUP_SOL int64 DATA_DADES object dtype: object
# Del dataset original, ens quedem només amb les columnes que farem servir, i agreguem els valors per barri.
camps = ['BARRI', 'NOM_BARRI', 'NUM_PARC', 'NUM_LOCALS', 'NUM_VIVENDES',
'NUM_PARKINGS', 'SUP_CONS', 'SUP_SOBRE_RASANT', 'SUP_SOTA_RASANT', 'SUP_SOL']
illes2 = illes[camps]
illes2 = illes2[illes2['NOM_BARRI'] != '??']
barris = illes2.groupby(['NOM_BARRI', 'BARRI']).sum().reset_index()
barris['BARRI'] = barris['BARRI'].astype(int)
barris = barris.rename(columns={'BARRI': 'Codi_Barri'})
barris = barris.sort_values("Codi_Barri").reset_index(drop=True)
barris.head()
| NOM_BARRI | Codi_Barri | NUM_PARC | NUM_LOCALS | NUM_VIVENDES | NUM_PARKINGS | SUP_CONS | SUP_SOBRE_RASANT | SUP_SOTA_RASANT | SUP_SOL | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | el Raval | 1 | 2001 | 33754 | 17767 | 3847 | 3058727 | 2698938 | 359789 | 848103 |
| 1 | el Barri Gòtic | 2 | 1360 | 17944 | 7331 | 1760 | 2385210 | 2050490 | 334720 | 645514 |
| 2 | la Barceloneta | 3 | 977 | 12208 | 7028 | 1324 | 1321287 | 962150 | 359137 | 994415 |
| 3 | Sant Pere, Santa Caterina i la Ribera | 4 | 1568 | 21521 | 10760 | 2194 | 2144102 | 1912488 | 231614 | 849889 |
| 4 | el Fort Pienc | 5 | 709 | 26886 | 13260 | 7708 | 2343840 | 2027625 | 316215 | 590000 |
# Carreguem el shapefile que conté els polígons dels barris i districtes de Barcelona.
geom_bcn = gpd.read_file('datasets\\shapefiles\\geom\\2.json')
geom_bcn.head()
| FID | ID_ANNEX | ANNEXDESCR | ID_TEMA | TEMA_DESCR | ID_CONJUNT | CONJ_DESCR | ID_SUBCONJ | SCONJ_DESC | ID_ELEMENT | ... | CODI_UA | TIPUS_UA | NOM | WEB1 | WEB2 | WEB3 | FHEX_COLOR | Shape_Leng | Shape_Area | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 01 | Grup - I | 0104 | Unitats Administratives | 010411 | Terme Municipal | 01041101 | Terme Municipal | 0104110101 | ... | 080193 | TERME | Barcelona | http://www.bcn.cat | http://www.bcn.cat/estadistica/catala/dades/in... | http://www.bcn.cat/estadistica/catala/document... | #000000 | 114036.624503 | 1.017050e+08 | MULTIPOLYGON (((432115.848 4590999.098, 432010... |
| 1 | 1 | 01 | Grup - I | 0104 | Unitats Administratives | 010412 | Districtes | 01041201 | Districte | 0104120101 | ... | 01 | DISTRICTE | Ciutat Vella | http://www.bcn.cat/ciutatvella | http://www.bcn.cat/estadistica/catala/dades/gu... | http://www.bcn.cat/estadistica/catala/document... | #000000 | 21366.961806 | 4.204931e+06 | POLYGON ((431733.736 4582441.816, 431645.093 4... |
| 2 | 2 | 01 | Grup - I | 0104 | Unitats Administratives | 010412 | Districtes | 01041201 | Districte | 0104120101 | ... | 02 | DISTRICTE | Eixample | http://www.bcn.cat/eixample | http://www.bcn.cat/estadistica/catala/dades/gu... | http://www.bcn.cat/estadistica/catala/document... | #000000 | 13931.644175 | 7.464303e+06 | POLYGON ((432033.184 4583665.032, 432033.186 4... |
| 3 | 3 | 01 | Grup - I | 0104 | Unitats Administratives | 010412 | Districtes | 01041201 | Districte | 0104120101 | ... | 03 | DISTRICTE | Sants-Montjuïc | http://www.bcn.cat/sants-montjuic | http://www.bcn.cat/estadistica/catala/dades/gu... | http://www.bcn.cat/estadistica/catala/document... | #000000 | 46711.856798 | 2.287985e+07 | MULTIPOLYGON (((428773.911 4580873.742, 428692... |
| 4 | 4 | 01 | Grup - I | 0104 | Unitats Administratives | 010412 | Districtes | 01041201 | Districte | 0104120101 | ... | 04 | DISTRICTE | Les Corts | http://www.bcn.cat/lescorts | http://www.bcn.cat/estadistica/catala/dades/gu... | http://www.bcn.cat/estadistica/catala/document... | #000000 | 12551.602001 | 6.010769e+06 | POLYGON ((425054.670 4583672.560, 425047.220 4... |
5 rows × 36 columns
# Filtrem i seleccionem només les unitats administratives tipus 'BARRI', ja que no farem servir els districtes ni altres tipus.
geom_barris = geom_bcn[geom_bcn['TIPUS_UA']== 'BARRI']
geom_barris = geom_barris[['CODI_UA', 'NOM', 'Shape_Leng', 'Shape_Area', 'geometry']]
geom_barris['CODI_UA'] = geom_barris['CODI_UA'].astype(int)
geom_barris = geom_barris.rename(columns={'CODI_UA': 'Codi_Barri'})
crs = geom_barris.crs
geom_barris.head()
| Codi_Barri | NOM | Shape_Leng | Shape_Area | geometry | |
|---|---|---|---|---|---|
| 11 | 1 | el Raval | 5521.646549 | 1.100286e+06 | POLYGON ((430162.188 4581936.985, 430102.838 4... |
| 12 | 2 | el Barri Gòtic | 5197.999887 | 8.155939e+05 | POLYGON ((431189.907 4581851.447, 431153.997 4... |
| 13 | 3 | la Barceloneta | 13853.129525 | 1.179382e+06 | POLYGON ((432798.734 4582081.260, 432794.049 4... |
| 14 | 4 | Sant Pere, Santa Caterina i la Ribera | 4664.482949 | 1.109669e+06 | POLYGON ((431733.736 4582441.816, 431645.093 4... |
| 15 | 5 | el Fort Pienc | 4137.328784 | 9.293558e+05 | POLYGON ((431741.815 4582625.649, 431771.957 4... |
# Unim les dades geomètriques al Dataframe general.
barris = pd.merge(barris, geom_barris, on='Codi_Barri')
# Convert DataFrame to a GeoDataFrame
gdf_barris = gpd.GeoDataFrame(barris, crs=crs, geometry=barris["geometry"])
# Carreguem els polígons dels barris sobre la base del mapa de Barcelona amb folium.
basebcn = folium.Map(location=[41.3887900, 2.1589900], zoom_start = 12, tiles="cartodb positron")
folium.GeoJson(data=gdf_barris["geometry"]).add_to(basebcn)
basebcn
En el primer punt del nostre anàlisi, ens centrarem en aspectes relacionats amb la densitat per habitants per hectàrea o de superfície construïda, comprovant si la densitat té algun tipus de relació amb el preu de l'habitatge. Per això, afegirem noves dades al dataset original, com l'àrea de cada barri o el preu de venda de l'habitatge.
# Carreguem el dataset original que conté les superfícies de cada barri en hectàrees.
densitat = pd.read_csv("datasets\\2021_densitat.csv")
densitat = densitat[['Codi_Barri', 'Població', 'Superfície (ha)', 'Superfície Residencial (ha)', 'Densitat (hab/ha)', 'Densitat neta (hab/ha)']]
densitat.head()
| Codi_Barri | Població | Superfície (ha) | Superfície Residencial (ha) | Densitat (hab/ha) | Densitat neta (hab/ha) | |
|---|---|---|---|---|---|---|
| 0 | 1 | 47228 | 110.0 | 49.6 | 429 | 952 |
| 1 | 2 | 21827 | 81.6 | 34.1 | 268 | 639 |
| 2 | 3 | 14643 | 117.9 | 13.5 | 124 | 1081 |
| 3 | 4 | 22131 | 111.0 | 32.4 | 199 | 683 |
| 4 | 5 | 31262 | 92.9 | 33.6 | 336 | 930 |
# Unim les dades de població, superfície, densitat i preu/m2 al dataset general.
gdf_barris = pd.merge(gdf_barris, densitat, on='Codi_Barri')
# Afegim la variable 'DENS_EDIF', de densitat edificatòria, dividint la superfície construïda total entre la superfície de sòl.
gdf_barris['DENS_EDIF'] = gdf_barris['SUP_CONS'] / gdf_barris['SUP_SOL']
gdf_barris.head()
| NOM_BARRI | Codi_Barri | NUM_PARC | NUM_LOCALS | NUM_VIVENDES | NUM_PARKINGS | SUP_CONS | SUP_SOBRE_RASANT | SUP_SOTA_RASANT | SUP_SOL | NOM | Shape_Leng | Shape_Area | geometry | Població | Superfície (ha) | Superfície Residencial (ha) | Densitat (hab/ha) | Densitat neta (hab/ha) | DENS_EDIF | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | el Raval | 1 | 2001 | 33754 | 17767 | 3847 | 3058727 | 2698938 | 359789 | 848103 | el Raval | 5521.646549 | 1.100286e+06 | POLYGON ((430162.188 4581936.985, 430102.838 4... | 47228 | 110.0 | 49.6 | 429 | 952 | 3.606551 |
| 1 | el Barri Gòtic | 2 | 1360 | 17944 | 7331 | 1760 | 2385210 | 2050490 | 334720 | 645514 | el Barri Gòtic | 5197.999887 | 8.155939e+05 | POLYGON ((431189.907 4581851.447, 431153.997 4... | 21827 | 81.6 | 34.1 | 268 | 639 | 3.695055 |
| 2 | la Barceloneta | 3 | 977 | 12208 | 7028 | 1324 | 1321287 | 962150 | 359137 | 994415 | la Barceloneta | 13853.129525 | 1.179382e+06 | POLYGON ((432798.734 4582081.260, 432794.049 4... | 14643 | 117.9 | 13.5 | 124 | 1081 | 1.328708 |
| 3 | Sant Pere, Santa Caterina i la Ribera | 4 | 1568 | 21521 | 10760 | 2194 | 2144102 | 1912488 | 231614 | 849889 | Sant Pere, Santa Caterina i la Ribera | 4664.482949 | 1.109669e+06 | POLYGON ((431733.736 4582441.816, 431645.093 4... | 22131 | 111.0 | 32.4 | 199 | 683 | 2.522802 |
| 4 | el Fort Pienc | 5 | 709 | 26886 | 13260 | 7708 | 2343840 | 2027625 | 316215 | 590000 | el Fort Pienc | 4137.328784 | 9.293558e+05 | POLYGON ((431741.815 4582625.649, 431771.957 4... | 31262 | 92.9 | 33.6 | 336 | 930 | 3.972610 |
mapa_bcn1 = folium.Map(location=[41.38, 2.16], zoom_start = 12, tiles=None, overlay=False)
densitat1 = folium.FeatureGroup(name='Densitat per barris (hab/ha)', overlay=False).add_to(mapa_bcn1)
densitat2 = folium.FeatureGroup(name='Densitat neta per barris (hab/ha)', overlay=False).add_to(mapa_bcn1)
densitat3 = folium.FeatureGroup(name='Densitat edificatòria', overlay=False).add_to(mapa_bcn1)
densitat_tot = folium.Choropleth(
geo_data=gdf_barris,
name="choropleth",
data=gdf_barris,
columns= ["NOM_BARRI", "Densitat (hab/ha)"],
key_on="feature.properties.NOM_BARRI",
fill_color="Reds",
fill_opacity=0.6,
line_opacity=0.0,
overlay=False,
legend_name="Densitat de població per barris").geojson.add_to(densitat1)
folium.features.GeoJson(
data=gdf_barris,
name='Densitat de població per barris',
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
tooltip=folium.features.GeoJsonTooltip(
fields=['NOM_BARRI',
'Població',
'Superfície (ha)',
'Densitat (hab/ha)'
],
aliases=["Barri:",
"Població:",
"Superfície (ha):",
"Densitat (hab/ha):",
],
localize=True,
sticky=False,
labels=True,
style="""background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,),
highlight_function=lambda x: {'weight':3,'fillColor':'grey'},
).add_to(densitat_tot)
densitat_neta =folium.Choropleth(
geo_data=gdf_barris,
name="choropleth",
data=gdf_barris,
columns= ["NOM_BARRI", "Densitat neta (hab/ha)"],
key_on="feature.properties.NOM_BARRI",
fill_color="Reds",
fill_opacity=0.6,
line_opacity=0.0,
overlay=False,
legend_name="Densitat neta de població per barris").geojson.add_to(densitat2)
folium.features.GeoJson(
data=gdf_barris,
name='Densitat neta de població per barris',
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
tooltip=folium.features.GeoJsonTooltip(
fields=['NOM_BARRI',
'Població',
'Superfície (ha)',
'Densitat neta (hab/ha)'
],
aliases=["Barri:",
"Població:",
"Superfície (ha):",
"Densitat neta (hab/ha):",
],
localize=True,
sticky=False,
labels=True,
style="""background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,),
highlight_function=lambda x: {'weight':3,'fillColor':'grey'},
).add_to(densitat_neta)
<folium.features.GeoJson at 0x159022facd0>
densitat_edif =folium.Choropleth(
geo_data=gdf_barris,
name="choropleth",
data=gdf_barris,
columns= ["NOM_BARRI", "DENS_EDIF"],
key_on="feature.properties.NOM_BARRI",
fill_color="RdPu",
fill_opacity=0.6,
line_opacity=0.0,
overlay=False,
legend_name="Densitat edificatòria per barris").geojson.add_to(densitat3)
folium.features.GeoJson(
data=gdf_barris,
name='Densitat de superfície construïda (sup. construïda / sup. solar)',
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
tooltip=folium.features.GeoJsonTooltip(
fields=['NOM_BARRI',
'DENS_EDIF',
'SUP_CONS',
'SUP_SOL'
],
aliases=["Barri:",
"Densitat edificatòria:",
"Superfície construïda:",
"Superfície solars:",
],
localize=True,
sticky=False,
labels=True,
style="""background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,),
highlight_function=lambda x: {'weight':3,'fillColor':'grey'},
).add_to(densitat_edif)
<folium.features.GeoJson at 0x15902a6acd0>
folium.TileLayer('cartodbpositron',overlay=True,name="Mapa base").add_to(mapa_bcn1)
folium.LayerControl(collapsed=False).add_to(mapa_bcn1)
# Mostrem el mapa generat
mapa_bcn1
fig = px.bar(gdf_barris.sort_values(by='Densitat (hab/ha)').dropna(),
y='NOM_BARRI',
x='Densitat (hab/ha)',
orientation='h',
color='DENS_EDIF',
color_continuous_scale='RdPu',
height=1600,
title="Densitat de població a Barcelona")
fig.update_layout(yaxis=dict(title='Barri'))
fig.show()
Mostrem els preus de l'habitatge a nivell de venda i lloguer.
preu_venda = pd.read_csv("datasets\\2022_comp_vend_preu_trim.csv")
preu_venda = preu_venda[preu_venda['Preu_mitja_habitatge']=='Total. Euros/m2 construït']
preu_venda['preu_venda_m2'] = preu_venda['Valor'].apply(pd.to_numeric, errors='coerce')
preu_venda = preu_venda.groupby(['Codi_Barri']).mean().reset_index()
preu_venda = preu_venda[['Codi_Barri', 'preu_venda_m2']]
df_lloguer = pd.read_csv("datasets\\2022_lloguer_preu_trim.csv")
preu_lloguer_mes = df_lloguer[df_lloguer['Lloguer_mitja']=='Lloguer mitjà mensual (Euros/mes)']
preu_lloguer_mes = preu_lloguer_mes.groupby(['Codi_Barri']).mean().reset_index()
preu_lloguer_m2 = df_lloguer[df_lloguer['Lloguer_mitja']=='Lloguer mitjà per superfície (Euros/m2 mes)']
preu_lloguer_m2 = preu_lloguer_m2.groupby(['Codi_Barri']).mean().reset_index()
preu_lloguer_mes = preu_lloguer_mes[['Codi_Barri', 'Preu']]
preu_lloguer_m2 = preu_lloguer_m2[['Codi_Barri', 'Preu']]
preu_lloguer_mes = preu_lloguer_mes.rename(columns={'Preu': 'preu_lloguer_mes'})
preu_lloguer_m2 = preu_lloguer_m2.rename(columns={'Preu': 'preu_lloguer_m2'})
preu_lloguer = pd.merge(preu_lloguer_mes, preu_lloguer_m2, on='Codi_Barri')
preu_lloguer.head()
| Codi_Barri | preu_lloguer_mes | preu_lloguer_m2 | |
|---|---|---|---|
| 0 | 1 | 905.700000 | 15.200000 |
| 1 | 2 | 1118.466667 | 15.900000 |
| 2 | 3 | 941.066667 | 18.500000 |
| 3 | 4 | 1053.033333 | 16.033333 |
| 4 | 5 | 1061.166667 | 14.233333 |
gdf_barris = pd.merge(gdf_barris, preu_venda, on='Codi_Barri')
gdf_barris = pd.merge(gdf_barris, preu_lloguer, on='Codi_Barri')
gdf_barris['preu_lloguer_venda'] = gdf_barris['preu_venda_m2'] / (gdf_barris['preu_lloguer_m2'] * 12)
gdf_barris.head()
| NOM_BARRI | Codi_Barri | NUM_PARC | NUM_LOCALS | NUM_VIVENDES | NUM_PARKINGS | SUP_CONS | SUP_SOBRE_RASANT | SUP_SOTA_RASANT | SUP_SOL | ... | Població | Superfície (ha) | Superfície Residencial (ha) | Densitat (hab/ha) | Densitat neta (hab/ha) | DENS_EDIF | preu_venda_m2 | preu_lloguer_mes | preu_lloguer_m2 | preu_lloguer_venda | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | el Raval | 1 | 2001 | 33754 | 17767 | 3847 | 3058727 | 2698938 | 359789 | 848103 | ... | 47228 | 110.0 | 49.6 | 429 | 952 | 3.606551 | 3378.800000 | 905.700000 | 15.200000 | 18.524123 |
| 1 | el Barri Gòtic | 2 | 1360 | 17944 | 7331 | 1760 | 2385210 | 2050490 | 334720 | 645514 | ... | 21827 | 81.6 | 34.1 | 268 | 639 | 3.695055 | 4711.200000 | 1118.466667 | 15.900000 | 24.691824 |
| 2 | la Barceloneta | 3 | 977 | 12208 | 7028 | 1324 | 1321287 | 962150 | 359137 | 994415 | ... | 14643 | 117.9 | 13.5 | 124 | 1081 | 1.328708 | 4705.400000 | 941.066667 | 18.500000 | 21.195495 |
| 3 | Sant Pere, Santa Caterina i la Ribera | 4 | 1568 | 21521 | 10760 | 2194 | 2144102 | 1912488 | 231614 | 849889 | ... | 22131 | 111.0 | 32.4 | 199 | 683 | 2.522802 | 4416.800000 | 1053.033333 | 16.033333 | 22.956341 |
| 4 | el Fort Pienc | 5 | 709 | 26886 | 13260 | 7708 | 2343840 | 2027625 | 316215 | 590000 | ... | 31262 | 92.9 | 33.6 | 336 | 930 | 3.972610 | 4229.133333 | 1061.166667 | 14.233333 | 24.760734 |
5 rows × 24 columns
mapa_bcn2 = folium.Map(location=[41.38, 2.16], zoom_start = 12, tiles=None, overlay=False)
preus_cv = folium.FeatureGroup(name='Preu compravenda habitatge €/m2', overlay=False).add_to(mapa_bcn2)
preus_lloguer = folium.FeatureGroup(name='Preu lloguer mensual habitatge €/mes', overlay=False).add_to(mapa_bcn2)
preus_lloguer_m2 = folium.FeatureGroup(name='Preu lloguer mensual habitatge €/m2/mes', overlay=False).add_to(mapa_bcn2)
preus_lloguer_venda = folium.FeatureGroup(name='Relació preu lloguer anual vs. venda', overlay=False).add_to(mapa_bcn2)
# Mapa preu venda
compravenda =folium.Choropleth(
geo_data=gdf_barris,
name="choropleth",
data=gdf_barris,
columns= ["NOM_BARRI", "preu_venda_m2"],
key_on="feature.properties.NOM_BARRI",
fill_color="RdPu",
fill_opacity=0.6,
line_opacity=0.0,
nan_fill_color="White",
overlay=False,
legend_name="Preu mitjà de compravenda d'habitatge per barris (€/m2)").geojson.add_to(preus_cv)
folium.features.GeoJson(
data=gdf_barris,
name='Preu mitjà de compravenda habitatge per barris (€/m2)',
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
tooltip=folium.features.GeoJsonTooltip(
fields=['NOM_BARRI',
'preu_venda_m2'
],
aliases=["Barri:",
"Preu compravenda habitatge (€/m2):"
],
localize=True,
sticky=False,
labels=True,
style="""background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,),
highlight_function=lambda x: {'weight':3,'fillColor':'grey'},
).add_to(compravenda)
# Mapa preu total lloguer mensual
lloguer =folium.Choropleth(
geo_data=gdf_barris,
name="choropleth",
data=gdf_barris,
columns= ["NOM_BARRI", "preu_lloguer_mes"],
key_on="feature.properties.NOM_BARRI",
fill_color="RdPu",
fill_opacity=0.6,
line_opacity=0.0,
nan_fill_color="White",
overlay=False,
legend_name="Preu mitjà de compravenda d'habitatge per barris (€/m2)").geojson.add_to(preus_lloguer)
folium.features.GeoJson(
data=gdf_barris,
name='Preu mitjà de lloguer habitatge per barris (€/mes)',
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
tooltip=folium.features.GeoJsonTooltip(
fields=['NOM_BARRI',
'preu_lloguer_mes'
],
aliases=["Barri:",
"Preu lloguer (€/mes):"
],
localize=True,
sticky=False,
labels=True,
style="""background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,),
highlight_function=lambda x: {'weight':3,'fillColor':'grey'},
).add_to(lloguer)
# Mapa preu m2 lloguer mensual
lloguerm2 =folium.Choropleth(
geo_data=gdf_barris,
name="choropleth",
data=gdf_barris,
columns= ["NOM_BARRI", "preu_lloguer_m2"],
key_on="feature.properties.NOM_BARRI",
fill_color="RdPu",
fill_opacity=0.6,
line_opacity=0.0,
nan_fill_color="White",
overlay=False,
legend_name="Preu mitjà de lloguer habitatge per barris (€/mes)").geojson.add_to(preus_lloguer_m2)
folium.features.GeoJson(
data=gdf_barris,
name='Preu mitjà de lloguer habitatge per barris (€/m2)',
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
tooltip=folium.features.GeoJsonTooltip(
fields=['NOM_BARRI',
'preu_lloguer_m2'
],
aliases=["Barri:",
"Preu lloguer mensual(€/m2):"
],
localize=True,
sticky=False,
labels=True,
style="""background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,),
highlight_function=lambda x: {'weight':3,'fillColor':'grey'},
).add_to(lloguerm2)
# Relació entre el preu anual del lloguer i el preu de compra.
lloguer_venda =folium.Choropleth(
geo_data=gdf_barris,
name="choropleth",
data=gdf_barris,
columns= ["NOM_BARRI", "preu_lloguer_venda"],
key_on="feature.properties.NOM_BARRI",
fill_color="RdPu",
fill_opacity=0.6,
line_opacity=0.0,
nan_fill_color="White",
overlay=False,
legend_name="Relació preu lloguer anual vs. venda").geojson.add_to(preus_lloguer_venda)
folium.features.GeoJson(
data=gdf_barris,
name='Relació lloguer anual vs. venda',
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
tooltip=folium.features.GeoJsonTooltip(
fields=['NOM_BARRI',
'preu_lloguer_venda'
],
aliases=["Barri:",
"Preu lloguer anual/Preu venda:"
],
localize=True,
sticky=False,
labels=True,
style="""background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,),
highlight_function=lambda x: {'weight':3,'fillColor':'grey'},
).add_to(lloguer_venda)
folium.TileLayer('cartodbpositron',overlay=True,name="Mapa base").add_to(mapa_bcn2)
folium.LayerControl(collapsed=False).add_to(mapa_bcn2)
# Mostrem el mapa generat
mapa_bcn2